package teach.aistar.rbm;


import cn.hutool.core.util.IdUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import teach.aistar.model.entity.WebOrder;
import teach.aistar.service.IOrderService;
import teach.aistar.service.ISeckillService;
import teach.aistar.service.IWebSeckillService;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 本类用来演示: 该任务订阅了
 *              j13_sec中的order_que的队列消息
 *
 * @author: success
 * @date: 2021/5/13 10:39 上午
 */
@Component
public class OrderRbmHandler {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ISeckillService iSeckillService;

    @Autowired
    private IWebSeckillService iWebSeckillService;

    @Autowired
    private RedisTemplate redisTemplate;



    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "order_que"),
            exchange = @Exchange(value = "j13_secKill",type = "fanout")))
    @RabbitHandler
    public void saveOrder(Map<String,Object> result, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("下单操作进行中...");
        //下单的操作... - 比较耗时间的操作...
//        WebOrder webOrder = new WebOrder();
        Long secId = (Long) result.get("secId");



        iSeckillService.update(secId);
//        String oderNo = UUID.randomUUID().toString();
        String oderNo = IdUtil.getSnowflake(1,1).nextIdStr();


        String uName = (String) result.get("uName");
        System.out.println("uName......"+uName);
        Double prices = (Double) result.get("prices");

        orderService.saveOrder(oderNo,uName,"0",secId,new Date(),prices);
//        iWebSeckillService.update();

        //手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "order_que1"),
            exchange = @Exchange(value = "j131_secKill",type = "fanout")))
    @RabbitHandler
    public void saveOrderz(Map<String,Object> result, @Headers Map<String,Object> headers, Channel channel){
        System.out.println("下单操作进行中...");
        //下单的操作... - 比较耗时间的操作...
//        WebOrder webOrder = new WebOrder();
        Long secId = (Long) result.get("secId");

        iSeckillService.update(secId);
//        String oderNo = UUID.randomUUID().toString();
        String oderNo = IdUtil.getSnowflake(1,1).nextIdStr();

        Double prices = (Double) result.get("prices");
        String uName = (String) result.get("uName");
        System.out.println("uName......"+uName);

        orderService.saveOrder(oderNo,uName,"0",secId,new Date(),prices);
//        iWebSeckillService.update();

        //手动ack
        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Autowired
    private JavaMailSender javaMailSender;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "order_message"),
            exchange = @Exchange(value = "j13_secKill",type = "fanout")))
    @RabbitHandler
    public void sendMessage(Map<String,Object> result,@Headers Map<String,Object> headers,Channel channel){

        SimpleMailMessage msg = new SimpleMailMessage();
        String email = (String) result.get("email");
        //邮箱的发送方 - 配置文件中的username保持一致
        msg.setFrom("1849730314@qq.com");
        String uName = (String) result.get("uName");
        Long secId = (Long) result.get("secId");
        //邮箱的标题
        msg.setSubject("龙腾有限公司:");




        //邮箱的正文内容
        //验证码随机数 - 6位(Math.random()*10);
//        String code = String.valueOf((int) (Math.random()*900000+100000));


        msg.setText("恭喜您:"+uName+"您参加的秒杀活动成功秒杀，祝您购物愉快");

//        ValueOperations vop = redisTemplate.opsForValue();
//        vop.set("user_message", code);
//
//        String user_message = (String) vop.get("user_message");
//
//        System.out.println("user_message........"+user_message);
        msg.setTo(email);

        //发送
        javaMailSender.send(msg);


        System.out.println("发送成功");

        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
